package com.xpec.c4.game.login;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Session;

import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.db.model.BlackListDetailModel;
import com.xpec.c4.game.utility.GUID;

/**
 * 黑名单管理
 * 
 * @author zhongzhengkai 2012-8-3
 */
public class BlackListManager {
	private static final Log log = LogFactory.getLog(BlackListManager.class);
	
	/**
	 *玩家黑名单列表
	 */
	private static HashMap<GUID, Long> map = new HashMap<GUID, Long>();

	//moved from BlackListDAO.loadAll to here on 2013/5/17
	//原本在ModuleServer startup的時候執行
	//改成有call到的才執行此init
	static {
		loadAll();
	}
	
	public static void loadAll() {
		Session session = HibernateManager.getSession();
//		List<BlackListDetailModel> model = session.createQuery(
//				"from BlackListDetailModel").list();
		Criteria criteria=session.createCriteria(BlackListDetailModel.class);
		List<BlackListDetailModel> model = criteria.list();
		HibernateManager.endSession();

		log.info("********** loading all black list account start **********");

		for (BlackListDetailModel m : model) {
			addBlackList(GUID.parseUUID(m.getAccountid()), m.getEndDate());
		}
		log.info("********** loading all black list account complete **********");
	}
	
	
	// 玩家的禁言增删改只对map操作，不对数据库操作，因为在这之前数据库的相应
	// 操作可能已被GMservice操作过，所以调用这些接口时需注意一下具体情况需不需要
	// 去操作数据库对玩家的禁言增删改
	public static void addBlackList(GUID guid, long endTime) {
		synchronized (guid) {
			map.put(guid, endTime);
		}
	}

	public static void removeBlackList(GUID guid) {
		synchronized (guid) {
		map.remove(guid);
		}
	}

	public static void updateBlackList(GUID guid, long endTime) {
		synchronized (guid) {
		map.put(guid, endTime);
		}
	}

	public static HashMap<GUID, Long> getBlackListMap() {
		return map;
	}

}
